package com.shangguigu.hadoopdashujv.pojo;

import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.Mapper;

import java.io.IOException;
import java.util.StringTokenizer;

public class WordCountMapper extends Mapper<LongWritable, Text, Text, IntWritable> {
    // 定义一个IntWritable类型的常量，值为1
    private final static IntWritable one = new IntWritable(1);
    // 定义一个Text类型的变量，用于存储单词
    private Text word = new Text();

    @Override
    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
        // 将输入的Text类型数据转换为字符串
        String line = value.toString();
        // 使用StringTokenizer类将字符串分割成单词
        StringTokenizer tokenizer = new StringTokenizer(line);

        // 遍历所有单词
        while (tokenizer.hasMoreTokens()) {
            // 将单词设置为Text类型变量
            word.set(tokenizer.nextToken());
            // 将单词和1写入上下文中
            context.write(word, one);
        }
    }
}
